package com.yuyou.train.lesson.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.yuyou.train.lesson.mapper.ResourceQuestionMapper;
import com.yuyou.train.util.enums.lesson.QuestionEnums;
import com.yuyou.train.util.tool.DefindBeanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuyou.train.lesson.mapper.ResourceExamItemMapper;
import com.yuyou.train.lesson.service.IResourceExamItemService;
import com.yuyou.train.lesson.service.IResourceExamService;
import com.yuyou.train.lesson.vo.req.ReqResExamItemAdd;
import com.yuyou.train.lesson.vo.req.ReqResExamItemUpdate;
import com.yuyou.train.service.api.lesson.dto.ExamItemDTO;
import com.yuyou.train.service.api.lesson.dto.ExamQuestionDTO;
import com.yuyou.train.service.api.lesson.entity.ResourceExam;
import com.yuyou.train.service.api.lesson.entity.ResourceExamItem;
import com.yuyou.train.util.bean.ResBoolSimpleInfo;
import com.yuyou.train.util.enums.CommEnums;
import com.yuyou.train.util.message.lesson.ExamMess;
import com.yuyou.train.util.tool.BeanUtils;
import com.yuyou.train.util.tool.DateUtil;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class ResourceExamItemServiceImpl implements IResourceExamItemService{

	@Autowired
	private ResourceExamItemMapper resourceExamItemMapper;

	@Autowired
	private ResourceQuestionMapper resourceQuestionMapper;
	
	@Autowired
	IResourceExamService resourceExamService;
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return resourceExamItemMapper.deleteByPrimaryKey(id);
	}

	@Override
	public ResBoolSimpleInfo addExamItem(ReqResExamItemAdd reqResExamItemAdd) {

		////试卷绑定课程不能修改
		ResBoolSimpleInfo<String> res=resourceExamService.hasLessonRel(reqResExamItemAdd.getResourceExamId());
		if(!res.isSuccess()){
			return res;
		}
		
		// 先判断题目数是否超出试卷题目总数
		ResourceExam resourceExam = resourceExamService.selectByPrimaryKey(reqResExamItemAdd.getResourceExamId());

		List<ResourceExamItem> examItemList = resourceExamItemMapper.findExamItemByExamId(reqResExamItemAdd.getResourceExamId());
		if(null != examItemList && !examItemList.isEmpty()){
			if(examItemList.size() >= resourceExam.getNum()){
				return new ResBoolSimpleInfo(false, ExamMess.ERR_EXAM_QUESTION_NUM);
			}
		}

		List<String> resourceQuestionIds = BeanUtils.toList(examItemList, "resourceQuestionId");
		if(resourceQuestionIds.contains(reqResExamItemAdd.getResourceQuestionId())){
			return new ResBoolSimpleInfo(false, ExamMess.ERR_EXAM_ITEM_EXIST);
		}

		ResourceExamItem resourceExamItem = new ResourceExamItem();
		DefindBeanUtils.copyProperties(reqResExamItemAdd, resourceExamItem);
		resourceExamItem.setScore(0); // 默认0分
		resourceExamItem.setSort(findExamItemCountByExamId(resourceExamItem.getResourceExamId()) + 1);

		resourceExamItem.setId(Sequence.getSequence().getNextId());
		resourceExamItem.setCreateTime(new Date());
		resourceExamItemMapper.insert(resourceExamItem);
		return ResBoolSimpleInfo.getSuccessInfo();
	}


	@Override
	public ResBoolSimpleInfo batchAddExamItem(String resourceExamId, List<String> resourceQuestionIds){
		
		//已经关联不能修改
		ResBoolSimpleInfo<String> res=resourceExamService.hasLessonRel(resourceExamId);
		if(!res.isSuccess()){
			res.setMessage("课程启用不能修改");
			return res;
		}

		int i = resourceQuestionMapper.countQuestionByIdsAndStatus(resourceQuestionIds, CommEnums.isUse.no.getValue());
		if(i > 0){
			return new ResBoolSimpleInfo(false, ExamMess.ERR_EXAM_ADD_QUESTION_ISUSE);
		}


		// 先判断题目数是否超出试卷题目总数
		ResourceExam resourceExam = resourceExamService.selectByPrimaryKey(resourceExamId);

		List<ResourceExamItem> examItemList = resourceExamItemMapper.findExamItemByExamId(resourceExamId);
		List<String> questionIdsQuery = BeanUtils.toList(examItemList, "resourceQuestionId");

		resourceQuestionIds.removeAll(questionIdsQuery);
		if(resourceQuestionIds.size() + questionIdsQuery.size() > resourceExam.getNum()){
			return new ResBoolSimpleInfo(false, ExamMess.ERR_EXAM_QUESTION_NUM);
		}

		for(String questionId : resourceQuestionIds){
			if(!questionIdsQuery.contains(questionId)){
				ResourceExamItem resourceExamItem = new ResourceExamItem();
				resourceExamItem.setId(Sequence.getSequence().getNextId());
				resourceExamItem.setResourceExamId(resourceExamId);
				resourceExamItem.setResourceQuestionId(questionId);
				resourceExamItem.setScore(0); // 默认0分
				resourceExamItem.setSort(findExamItemCountByExamId(resourceExamItem.getResourceExamId()) + 1);
				resourceExamItem.setCreateTime(new Date());
				resourceExamItemMapper.insert(resourceExamItem);
			}
		}
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ResourceExamItem selectByPrimaryKey(String id) {
		return resourceExamItemMapper.selectByPrimaryKey(id);
	}

	@Override
	public ResBoolSimpleInfo<String> updateExamItem(ReqResExamItemUpdate reqResExamItemUpdate) {
		
		//已经关联不能修改
		ResBoolSimpleInfo<String> res=resourceExamService.hasLessonRel(reqResExamItemUpdate.getResourceExamId());
		if(!res.isSuccess()){
			return res;
		}
		
		ResourceExamItem resourceExamItem = new ResourceExamItem();
		resourceExamItem.setId(reqResExamItemUpdate.getExamItemId());
		resourceExamItem.setScore(reqResExamItemUpdate.getScore());

		// 先判断题目分数是否超出试卷题目总分
		ResourceExam resourceExam = resourceExamService.selectByPrimaryKey(reqResExamItemUpdate.getResourceExamId());
		int scoreSum = reqResExamItemUpdate.getScore();
		List<ResourceExamItem> examItemList = resourceExamItemMapper.findExamItemByExamId(reqResExamItemUpdate.getResourceExamId());
		if(null != examItemList && !examItemList.isEmpty()){
			for(ResourceExamItem examItem : examItemList){
				if(!examItem.getId().equals(reqResExamItemUpdate.getExamItemId())){
					scoreSum += examItem.getScore();
				}
			}
		}
		if(scoreSum > resourceExam.getScore()){
			return new ResBoolSimpleInfo(false, ExamMess.ERR_EXAM_QUESTION_SCORE);
		}

		resourceExamItem.setUpdateTime(new Date());
		resourceExamItemMapper.updateByPrimaryKeySelective(resourceExamItem);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	@Transactional(timeout=60)
	public ResBoolSimpleInfo<String> delExamItem(String id) {
			
		//试卷已经启动不能删除
		ResourceExamItem item = this.selectByPrimaryKey(id);
		ResourceExam resourceExam = resourceExamService.selectByPrimaryKey(item.getResourceExamId());
		if(resourceExam.getIsUse().equals(CommEnums.isOrNot.yes.getValue())){
			return new ResBoolSimpleInfo<String>(false,ExamMess.ERR_DEL_EXAM_USE);
		}
		
		//已经关联不能修改
		ResBoolSimpleInfo<String> res=resourceExamService.hasLessonRel(item.getResourceExamId());
		if(!res.isSuccess()){
			return res;
		}
		
		this.deleteByPrimaryKey(id);

		reSortExamItem(resourceExam.getId());
		return ResBoolSimpleInfo.getSuccessInfo();
	}


	@Override
	public void reSortExamItem(String examId){
		List<ResourceExamItem> examItemList = resourceExamItemMapper.findExamItemByExamId(examId);
		Collections.sort(examItemList, new Comparator<ResourceExamItem>() {
			@Override
			public int compare(ResourceExamItem o1, ResourceExamItem o2) {
				return o1.getSort() - o2.getSort();
			}
		});

		for(int i = 0; i < examItemList.size() ; i++){
			ResourceExamItem examItemUpdate = new ResourceExamItem();
			examItemUpdate.setId(examItemList.get(i).getId());
			examItemUpdate.setSort(i + 1);
			examItemUpdate.setUpdateTime(DateUtil.now());

			resourceExamItemMapper.updateByPrimaryKeySelective(examItemUpdate);
		}
	}


	@Override
	public ExamItemDTO findExamItemWithQuestionByExamId(String examId) {
		ExamItemDTO examItemDTO = new ExamItemDTO();
		List<ExamQuestionDTO> examQuestionList = resourceExamItemMapper.findExamItemWithQuestionByExamId(examId);
		examItemDTO.setExamQuestionList(examQuestionList);

		ResourceExam resourceExam = resourceExamService.selectByPrimaryKey(examId);
		examItemDTO.setQuestionSumOfExam(resourceExam.getNum());
		examItemDTO.setScoreSumOfExam(resourceExam.getScore());

		// 试卷明细情况
		int questionSum = 0;
		int scoreSum = 0;
		for(ExamQuestionDTO examQuestionDTO : examQuestionList){
			questionSum ++;
			scoreSum += examQuestionDTO.getScore();
		}

		examItemDTO.setScoreSum(scoreSum);
		examItemDTO.setQuestionSum(questionSum);
		return examItemDTO;
	}

	@Override
	public List<ResourceExamItem> findExamItemByQuestionId(String questionId) {
		return resourceExamItemMapper.findExamItemByQuestionId(questionId);
	}

	@Override
	public List<ResourceExamItem> findExamItemByExamId(String examId) {
		return resourceExamItemMapper.findExamItemByExamId(examId);
	}

	@Override
	public int findExamItemCountByExamId(String examId){
		return resourceExamItemMapper.findExamItemCountByExamId(examId);
	}

	@Override
	public ResBoolSimpleInfo changeExamItemSort(String origExamItemId, String destExamItemId){
		// 交换题目顺序
		ResourceExamItem origExamItem = selectByPrimaryKey(origExamItemId);
		ResourceExamItem destExamItem = selectByPrimaryKey(destExamItemId);

		if(null == origExamItem || null == destExamItem){
			return new ResBoolSimpleInfo(false, ExamMess.ERR_EXAM_ITEM_NOT_EXIST);
		}

		ResourceExamItem examItem = new ResourceExamItem();
		examItem.setId(origExamItem.getId());
		examItem.setSort(destExamItem.getSort());
		examItem.setUpdateTime(DateUtil.now());
		resourceExamItemMapper.updateByPrimaryKeySelective(examItem);

		examItem.setId(destExamItem.getId());
		examItem.setSort(origExamItem.getSort());
		examItem.setUpdateTime(DateUtil.now());
		resourceExamItemMapper.updateByPrimaryKeySelective(examItem);

		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public ResourceExamItem selectExamItem(String resourceExamId,
			String resourceQuestionId, Integer sort) {
		return resourceExamItemMapper.selectExamItem(resourceExamId, resourceQuestionId, sort);
	}
}